package ctf.evaluation.SimulatorTests;

import java.io.IOException;

import ctf.evaluation.Point;
import ctf.evaluation.TestCase;
import ctf.evaluation.simulator.ConnectionManager;
import ctf.evaluation.simulator.ConnectionManager.STEALState;
import ctf.evaluation.simulator.data.Flag;
import ctf.evaluation.simulator.data.Player;
import ctf.evaluation.simulator.data.State;
import ctf.evaluation.simulator.responses.CommandResponse;
import ctf.evaluation.simulator.responses.GameOverException;
import ctf.evaluation.simulator.responses.RoundOverException;
import ctf.evaluation.simulator.responses.StateResponse;
import ctf.model.Side;
import ctf.network.NetworkException;
import ctf.network.ProtocolError;
import ctf.network.tcp.Connection;

public class FlagTests extends TestCase{
	private ConnectionManager connections = ConnectionManager.instance();
	private Connection red, blue;
	
	@Override
	public void setUp() {
		try {

			connections.enterState(STEALState.PLAY);	
			blue = connections.blue();
			red = connections.red();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

	}
	
	@Override public void tearDown() {};

	public void testCaptureFlag() throws IOException, NetworkException, ProtocolError, RoundOverException, GameOverException{

		
		Point playerPosition = new Point(105, 125);
		blue.sendLine("placePlayer 0 " + playerPosition .toSTEAL());
		CommandResponse response = new CommandResponse(blue);

		blue.sendLine("accelerate 0 0 2.0");
		response = new CommandResponse(blue);

		sleep(6000);
		
		
		blue.sendLine("getState");
		State blueState = (new StateResponse(blue)).state();
		
		Player player = blueState.team(Side.RIGHT).player(0);
		Flag flag = blueState.team(Side.LEFT).flag();
		
		assertEquals("Player position should be equal to flag position", flag.location().x, player.location().x);
		
		
		
	}
	
	/** Place a flag at a specific point */
	protected void place(Connection connection, Point point)
		throws IOException, NetworkException
	{
		connection.sendLine("placeFlag " + point.toSTEAL());
	}
	
	
}
